package net.sourceforge.simcpux;

import net.sourceforge.simcpux.util.MD5;
import net.sourceforge.simcpux.util.RandomStringGenerator;
import android.util.Log;

import com.firefly.app.cordova.dto.SytPayResult;
import com.firefly.app.cordova.enums.SytPayEnum;
import com.firefly.mobile.app.context.SytApplication;
import com.tencent.mm.sdk.constants.Build;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayUtil {
	
	public static String appid = "wx0bf7c52958ebc15d";

	public static SytPayResult pay(String prepayId){
//		prepayId = "wx201512182010324d2029654e0955227941";
		
		
        try{
        	IWXAPI 	api = WXAPIFactory.createWXAPI(SytApplication.context, appid);
        	boolean isPaySupported = api.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT;
        	SytPayResult sytPayResult = new SytPayResult();
    		if (!isPaySupported) {
    			sytPayResult.setCode(SytPayEnum.未安装.getCode());
    			sytPayResult.setMsg(SytPayEnum.未安装.getDesc());
    			return sytPayResult;
    		}
        	
        	String key = "1234567890qwertyuioasdfghjk12345";
			PayReq req = new PayReq();
			req.appId			= appid;
			req.partnerId		= "1345506401";
			req.prepayId		= prepayId;
			req.nonceStr		= RandomStringGenerator.getRandomStringByLength(16);
			req.timeStamp		= System.currentTimeMillis()/1000+"";
			req.packageValue	= "Sign=WXPay"; 
//			req.extData			= ""; // optional
			
			String orig = "appid="+req.appId+"&"
					+"noncestr="+req.nonceStr+"&"
					+"package="+req.packageValue+"&"
					+"partnerid="+req.partnerId+"&"
					+"prepayid="+req.prepayId+"&"
					+"timestamp="+req.timeStamp+"&"
					+"key="+key;
			req.sign			= MD5.MD5Encode(orig).toUpperCase();
			Log.i("PAY_GET", "正常调起支付："+orig);
//							Toast.makeText(PayActivity.this, "正常调起支付", Toast.LENGTH_SHORT).show();
			// 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
			boolean  ret = api.sendReq(req);
			
			Log.i("PAY_GET", "调支付结果："+ret);

//			String url = "http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=android";
//				byte[] buf = Util.httpGet(url);
//				if (buf != null && buf.length > 0) {
//					String content = new String(buf);
//					Log.e("get server pay params:",content);
//		        	JSONObject json = new JSONObject(content); 
//					if(null != json && !json.has("retcode") ){
//						PayReq req = new PayReq();
//						//req.appId = "wxf8b4f85f3a794e77";  // 测试用appId
//						req.appId			= json.getString("appid");
//						req.partnerId		= json.getString("partnerid");
//						req.prepayId		= json.getString("prepayid");
//						req.nonceStr		= json.getString("noncestr");
//						req.timeStamp		= json.getString("timestamp");
//						req.packageValue	= json.getString("package");
//						req.sign			= json.getString("sign");
//						req.extData			= "app data"; // optional
//						// 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
//						api.sendReq(req);
//					}
//				}
				
			sytPayResult.setCode(SytPayEnum.成功.getCode());
			sytPayResult.setMsg(SytPayEnum.成功.getDesc());
			return sytPayResult;
        }catch(Exception e){
        	e.printStackTrace();
        	Log.e("PAY_GET", "异常："+e.getMessage());
        	return  new SytPayResult();
//		        	Toast.makeText(PayActivity.this, "异常："+e.getMessage(), Toast.LENGTH_SHORT).show();
        }
	}
}
